import 'package:get/get.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/model/device_model.dart';
import 'package:yt_devops/model/info_item.dart';
import 'package:yt_devops/model/task_detail_model.dart';
import 'package:yt_devops/model/task_tail_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/func_utils.dart';

class ReportTaskDetailModel extends TaskDetailModel<ReportTaskStatusType> {
  @override
  ReportTaskStatusType? status;

  int? siteId;

  String? siteName;
  // 关联的维护单的id
  int? maintenanceId;

  ReportTaskDetailModel(
      {super.id,
      super.type,
      super.name,
      super.address,
      super.content,
      super.handlerBy,
      super.orgId,
      super.phone,
      super.timeOfAppointment,
      super.userId,
      super.userName,
      super.createName,
      super.handlerName,
      super.orgName,
      super.descPicUrl,
      super.processingResults,
      this.siteId,
      this.siteName,
      this.status,
      super.tails,
      this.maintenanceId});

  factory ReportTaskDetailModel.fromJson(Map<String, dynamic> json) =>
      ReportTaskDetailModel(
        id: json['id'],
        name: json['name'],
        siteId: json['siteId'],
        siteName: json['siteName'],
        address: json['address'],
        content: json['content'],
        handlerBy: json['handlerBy'],
        orgId: json['orgId'],
        phone: json['phone'],
        timeOfAppointment: json['timeOfAppointment'],
        userId: json['userId'],
        orgName: json['orgName'],
        handlerName: json['handlerName'],
        userName: json['userName'],
        createName: json['createName'],
        descPicUrl: json['descPicUrl'],
        processingResults: json['processingResults'],
        maintenanceId: json['maintenanceId'],
        tails: List.of(
          json['tails'],
        ).map((e) => TaskTailModel.fromJson(e)).toList(),
        status:
            $enumDecodeNullable($ReportTaskStatusTypeEnumMap, json['status']),
        type: $enumDecodeNullable($TaskTypeEnumMap, json['type']),
      );

  //基础信息
  List<InfoItem> get baseInfos {
    return [
      InfoItem(ResKey.siteName.tr, siteName ?? ''),
      InfoItem(ResKey.failureTitle.tr, name ?? ''),
    ];
  }

  //进度信息
  // 保障工单
  //create("创建客户报障工单",0), //创建工单事件
// createMatainance("创建维护工单", 4),
// finish("完成报障工单",2);  //完成工单事件
// finishMatainance("完成维护工单",7);
  List<TaskTailModel> get tasilInfos {
    var createMatainanceIdx = tails?.any((element) => element.eventId == 4);
    List<TaskTailModel> taillList = [];

    if (tails != null) {
      var isFinish = tails?.any((element) => element.eventId == 2);
      if (isFinish == true) {
        taillList = [
          TaskTailModel(name: ResKey.reporter.tr, eventId: 0),
          TaskTailModel(name: ResKey.finished.tr, eventId: 2),
        ];
      } else {
        taillList = [
          TaskTailModel(name: ResKey.reporter.tr, eventId: 0),
          TaskTailModel(name: ResKey.repairing.tr, eventId: 4),
          TaskTailModel(name: ResKey.repaired.tr, eventId: 7),
        ];
      }
      for (var i = 0; i < tails!.length; i++) {
        var index = taillList
            .indexWhere((element) => element.eventId == tails![i].eventId);
        if (index > -1) {
          tails![i].name = taillList[i].name;
          taillList[i] = tails![i];
        }
      }
    }

    return taillList;
  }

  // 问题描述
  String get discroption {
    return content ?? '';
  }

  // 附件
  List<String>? get pics {
    List<String> picList = [];
    picList = FuncUtils.isEmpty(descPicUrl) ? [] : descPicUrl?.split(',') ?? [];

    return picList;
  }

  // 处理意见
  String? get advice {
    if (status == ReportTaskStatusType.repairCompleted) {
      return processingResults ?? '';
    } else {
      return null;
    }
  }

  // 处理结果
  InfoItem? get result {
    var label = '处理结果';
    if (status == ReportTaskStatusType.repairCompleted) {
      return InfoItem(label, '已完成');
    } else {
      return null;
    }
  }

  //接收人
  bool get isShowReceiver {
    if (status == ReportTaskStatusType.waitingForProcessing) {
      return true;
    } else {
      return false;
    }
  }
}
